/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bluetoothserver.dataStore;

import bluetoothserver.graphical.IQMenu;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Collection of all of the answers sent via the bluetooth
 * @author Philip
 * @version 1.0 Final
 */
public class AnswerCollection {
    private static volatile AnswerCollection instance;
    private ArrayList<Answer> answers;
    private IQMenu menu = null;

    /**
     * Singleton constructor so that only one answer collection is present
     */
    private AnswerCollection(){
        answers = new ArrayList<Answer>();
    }

    /**
     * static instance getter
     * @return instance of the class
     */
    public static AnswerCollection getInstance(){
        if(instance == null){
            synchronized(AnswerCollection.class){
                if(instance == null){
                    instance = new AnswerCollection();
                }
            }
        }
        return instance;
    }

    /**
     * adds answer to the collection
     * @param a Answer
     */
    public void addAnswer(Answer a){
        answers.add(a);
        if(menu != null){
            menu.updateCounter();
        }
    }

    /**
     * returns the answer according to the index number
     * @param i
     * @return answer in the array
     */
    public Answer getAnswers(int i){
        return answers.get(i);
    }

    /**
     * returns all of the answers in an arraylist
     * @return arraylist
     */
    public ArrayList<Answer> getAllAnswers(){
        return answers;
    }

    /**
     * clears the entire arraylist when the question changes
     */
    public void clearAnswers(){
        answers.clear();
    }

    /**
     * searchs for the answers in the arraylist
     * @param s answer name 
     * @return number of answers in the arraylist
     */
    public int searchAnswers(String s){
        int total = 0;
        Iterator<Answer> i = answers.iterator();
        while(i.hasNext()){
            Answer a = i.next();
            if(s.compareTo(a.getAnswer())==0){
                total++;
            }
        }
        return total;
    }

    public Iterator getIterator(){
        return answers.iterator();
    }

    public int getSize(){
        return answers.size();
    }

    public void setMenu(IQMenu m){
        menu = m;
    }
}
